#include "stdafx.h"
#include "GameBrowserLeftSizedPanel.h"
#include "GameBrowserUtil.h"
#include "../resource.h"

BOOL GB_BUTTON_POS = FALSE;

IMPLEMENT_DYNAMIC(CGameBrowserLeftSizedPanel, CWnd)

CGameBrowserLeftSizedPanel::CGameBrowserLeftSizedPanel()
{
    m_bEnterDragChangeSize = false;
    m_cContainerSize.SetSize(100,100);
    m_pPanelControl = NULL;
    m_bInputIsValidate = FALSE;

    m_pButtonItemInfo = new BButtonItemInfo();
    m_pButtonItemInfo->rcPosition.SetRect(0,0,0,0);
    m_pButtonItemInfo->bMouseHoverd = FALSE;
    m_pButtonItemInfo->bWindowCollapsed = FALSE;
}

CGameBrowserLeftSizedPanel::~CGameBrowserLeftSizedPanel()
{
    if (m_pButtonItemInfo != NULL)
    {
        delete m_pButtonItemInfo;
        m_pButtonItemInfo = NULL;
    }
}


BEGIN_MESSAGE_MAP(CGameBrowserLeftSizedPanel, CWnd)
    //{{AFX_MSG_MAP(CDrawFrame)
     ON_WM_CREATE() 
     ON_WM_ERASEBKGND()
    ON_WM_SIZE()
    ON_WM_NCACTIVATE()
    ON_WM_NCHITTEST()
    ON_WM_NCLBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    ON_WM_NCMOUSEMOVE()
    ON_WM_NCLBUTTONUP()
    ON_WM_LBUTTONUP()
    ON_WM_PAINT()
    ON_MESSAGE(WM_IDLEUPDATECMDUI, &CGameBrowserLeftSizedPanel::OnIdleUpdateCmdUI)
   // ON_MESSAGE(WM_CHANGE_PANELCONTROLSIZE,OnChangePanelControlSize)
    //}}AFX_MSG_MAP

END_MESSAGE_MAP()


void CGameBrowserLeftSizedPanel::SetPanelControl(CWnd *pChild)
{
    pChild->SetParent(this);
    m_pPanelControl = pChild;

    CRect rcClient;
    GetWindowRect(rcClient);
    //OnSize(SIZE_RESTORED,rcClient.Width(),rcClient.Height());

    SendMessage(WM_SIZE,SIZE_RESTORED,MAKEWPARAM(rcClient.Width(),rcClient.Height()));

}
void CGameBrowserLeftSizedPanel::RemovePanelControl()
{
    m_pPanelControl = NULL;
}

int CGameBrowserLeftSizedPanel::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    m_cContainerSize.SetSize(GM_LEFT_PANEL_WIDTH,10);

    return TRUE;
}

void CGameBrowserLeftSizedPanel::OnSize(UINT nType, int cx, int cy)
{

    CWnd::OnSize(nType, cx, cy);  

    //asynchronous to change interop window's size
    //PostMessage(WM_CHANGE_PANELCONTROLSIZE,0,0);
}

void CGameBrowserLeftSizedPanel::OnSizing(UINT fwSide, LPRECT pRect)
{

}

 BOOL CGameBrowserLeftSizedPanel::OnEraseBkgnd(CDC*)
 {
     return TRUE;
 }

 LRESULT CGameBrowserLeftSizedPanel::OnIdleUpdateCmdUI(WPARAM wParam, LPARAM lParam)
 {      
     CPoint pt;
    ::GetCursorPos(&pt);
     ScreenToClient(&pt);
     if (m_pButtonItemInfo->rcPosition.PtInRect(pt))
     {
         if (m_pButtonItemInfo->bMouseHoverd == FALSE)
         {
            m_pButtonItemInfo->bMouseHoverd = TRUE;
             InvalidateRect(NULL,TRUE);
         }
     }
     else
     {
         if (m_pButtonItemInfo->bMouseHoverd == TRUE)
         {
             m_pButtonItemInfo->bMouseHoverd = FALSE;
             InvalidateRect(NULL,TRUE);
         }
     }

     return TRUE;

 }

void CGameBrowserLeftSizedPanel::OnPaint()
{
    CPaintDC dc(this); // device context for painting

    // TODO: Add your message handler code here

    CRect rt;
    GetClientRect(rt);
    //CBrush bruEdge(g_CColorEdge);
    CRect rcRight(rt.right - g_CEdgeWidth,0,rt.right,rt.bottom);

    //dc.FillRect(rcRight,&bruEdge);

    CDC hdcMem;
    hdcMem.CreateCompatibleDC(&dc);


    HBITMAP hbmScreen = CreateCompatibleBitmap(dc,rcRight.Width(),rcRight.Height()) ;
    HBITMAP hOldBmp = (HBITMAP)SelectObject(hdcMem,hbmScreen);

    //Draw all the info on the memory DC
    PaintOnMemoryDC(hdcMem);

    //Set Button Position    
    m_pButtonItemInfo->rcPosition.left = rcRight.left;
    m_pButtonItemInfo->rcPosition.right = rcRight.right;

    BOOL bRet = BitBlt(dc,rcRight.left,rcRight.top,rcRight.Width(),rcRight.Height(),hdcMem,0,0,SRCCOPY);

    SelectObject(hdcMem,hOldBmp);
    DeleteObject(hbmScreen);


    
}

void CGameBrowserLeftSizedPanel::PaintOnMemoryDC(CDC &hdcMem)
{
    CRect rt;
    GetClientRect(rt);
    CBrush bruEdge(g_CColorEdge);
    CRect rcRight(0,0,g_CEdgeWidth,rt.bottom);

    hdcMem.FillRect(rcRight,&bruEdge);

    int xWidthMid = rcRight.Height()/2;

    CBitmap   bitmapLeft; 

    if (m_pButtonItemInfo->bMouseHoverd == TRUE)
    {
        if (m_pButtonItemInfo ->bWindowCollapsed == FALSE)
        {
            bitmapLeft.LoadBitmap(IDB_BMP_LEFT_CHANNEL_CLICK_HOVER_P);
        }
        else
        {
            bitmapLeft.LoadBitmap(IDB_BMP_LEFT_CHANNEL_CLICK_HOVER_C);
        }
    }
    else
    {
        if (m_pButtonItemInfo ->bWindowCollapsed == FALSE)
        {
            bitmapLeft.LoadBitmap(IDB_BMP_LEFT_CHANNEL_CLICK_NORMAL_P);
        }
        else
        {
            bitmapLeft.LoadBitmap(IDB_BMP_LEFT_CHANNEL_CLICK_NORMAL_C);
        }
    }

    
    CDC   tempDC; 
    tempDC.CreateCompatibleDC(&hdcMem); 
    tempDC.SelectObject(&bitmapLeft); 
    BITMAP   Bmp;   
    bitmapLeft.GetBitmap(&Bmp);  
    int yPos = xWidthMid - Bmp.bmHeight/2; 

    //Set Button Position    
    m_pButtonItemInfo->rcPosition.top = yPos;
    m_pButtonItemInfo->rcPosition.bottom = yPos + Bmp.bmHeight;


    hdcMem.StretchBlt(0,yPos,rcRight.Width(),Bmp.bmHeight,&tempDC,0,0,Bmp.bmWidth,Bmp.bmHeight,SRCCOPY);    
    bitmapLeft.DeleteObject(); 


}

LRESULT CGameBrowserLeftSizedPanel::OnNcHitTest(CPoint point)
{  

    POINT ptScreen = {point.x,point.y};
    ScreenToClient(&ptScreen);

    if (m_pButtonItemInfo->rcPosition.PtInRect(ptScreen))
    {
        GB_BUTTON_POS = TRUE;
        return HTCLIENT;
    }

    if (GB_BUTTON_POS)
    {
        InvalidateRect(NULL,TRUE);
        GB_BUTTON_POS = FALSE;
    }
    


    if (ptScreen.x > m_cContainerSize.cx - 3)
    {
        return HTRIGHT;
    }



    return HTCLIENT;

}

void CGameBrowserLeftSizedPanel::OnNcLButtonDown(UINT nHitTest, CPoint point)
{

    //TRACE("NcLButtonDown\n");

    m_bEnterDragChangeSize = TRUE;

    m_OldSplitterPoint.x = m_OldSplitterPoint.y = 0;

    m_cLButtonDownBeginPoint = point;

    m_bInputIsValidate = FALSE;

    this->SetCapture();
}

BOOL CGameBrowserLeftSizedPanel::CheckValidateInput(POINT pt)
{
    //int nWidth = pt.x - m_cLButtonDownBeginPoint.x;
    //int nHeight = pt.y - m_cLButtonDownBeginPoint.y;

    //int nCurrentWidth = m_cContainerSize.cx + nWidth;
    //int nCurrentHeight = m_cContainerSize.cy + nHeight;

    CWnd *pMainWnd = AfxGetMainWnd();
    CRect rcMainWndRect;
    pMainWnd->GetWindowRect(rcMainWndRect);

    int nMaxWidth = rcMainWndRect.Width()/2;
    int nMaxHeight = rcMainWndRect.Height()/2;


    int nWidth = pt.x - m_cLButtonDownBeginPoint.x; 
    int nCurrentWidth = m_cContainerSize.cx + nWidth;
    if ( (nCurrentWidth <= SHP_MIN_WIDTH ) || (nCurrentWidth > nMaxWidth))
    {
        m_bValidateInput = FALSE;
        return FALSE;
    }           



    m_bValidateInput = TRUE;
    return TRUE;    
}

void CGameBrowserLeftSizedPanel::DrawSplitterLine(POINT ptCursor,BOOL bRemoveOld)
{
    HDC hdc = ::GetDC(NULL);
    CBrush* pBrush = CDC::GetHalftoneBrush();
    HBRUSH hOldBrush = NULL;

    if (pBrush != NULL)
    {
        hOldBrush = (HBRUSH)SelectObject(hdc, pBrush->m_hObject);
    }

    CRect rcWindowRect;
    this->GetWindowRect(rcWindowRect);

    if (m_OldSplitterPoint.x != 0 || m_OldSplitterPoint.y != 0)
    {
        CRect rcOld;


        rcOld.left = m_OldSplitterPoint.x - 2;
        rcOld.top = rcWindowRect.top;
        rcOld.bottom = rcWindowRect.bottom;
        rcOld.right = m_OldSplitterPoint.x + 2;  



        ::PatBlt(hdc,rcOld.left, rcOld.top, rcOld.Width(), rcOld.Height(), PATINVERT);
    }

    if (bRemoveOld != TRUE)
    { 
        m_bInputIsValidate = TRUE;
        m_OldSplitterPoint.x = ptCursor.x;
        m_OldSplitterPoint.y = ptCursor.y;

        CRect rcNew;


        rcNew.left = ptCursor.x - 2;
        rcNew.top = rcWindowRect.top;
        rcNew.bottom = rcWindowRect.bottom;
        rcNew.right = ptCursor.x + 2;  



        ::PatBlt(hdc,rcNew.left, rcNew.top, rcNew.Width(), rcNew.Height(), PATINVERT);
    }

    if (hOldBrush != NULL)
    {
        SelectObject(hdc, hOldBrush);
    }
    ::ReleaseDC(NULL,hdc);

}
void CGameBrowserLeftSizedPanel::OnMouseMove(UINT nFlags, CPoint point)
{
    if (GB_BUTTON_POS)
    {
        if (m_pButtonItemInfo->bMouseHoverd == FALSE)
        {
            m_pButtonItemInfo->bMouseHoverd = TRUE;
            InvalidateRect(NULL,TRUE);
        }
        CWnd::OnMouseMove(nFlags, point);
        return;
    }

    if (m_pButtonItemInfo->bMouseHoverd == TRUE)
    {
        m_pButtonItemInfo->bMouseHoverd = FALSE;
        InvalidateRect(NULL,TRUE);
    }
    //TRACE("MoseMove\n");
    if (m_bEnterDragChangeSize != TRUE)
    {
        CWnd::OnMouseMove(nFlags, point);
        return;
    }

   
    POINT ptCursor;
    ::GetCursorPos(&ptCursor);
   

    if (CheckValidateInput(ptCursor) == FALSE)
    {
        return;
    }

    DrawSplitterLine(ptCursor);   

    CWnd::OnMouseMove(nFlags, point);
}

void CGameBrowserLeftSizedPanel::OnNcMouseMove(UINT nHitTest, CPoint point)
{ 

    //TRACE("NcMouseMove\n");
    CWnd::OnNcMouseMove(nHitTest, point);
}

void CGameBrowserLeftSizedPanel::OnNcLButtonUp(UINT nHitTest, CPoint point)
{
    //TRACE("NcLButtonUp\n");


    CWnd::OnNcLButtonUp(nHitTest, point);
}

BOOL CGameBrowserLeftSizedPanel::ChangeClientWindowSize(POINT ptClient)
{

    int nWidth = ptClient.x - m_cLButtonDownBeginPoint.x;
    if (nWidth == 0)
    {
        return FALSE;
    }
    m_cContainerSize.cx += nWidth;

    return TRUE;

}
void CGameBrowserLeftSizedPanel::OnLButtonUp(UINT nFlags, CPoint point)
{

    TRACE("LButtonUp\n");
    
    ::ReleaseCapture();

    if(m_bEnterDragChangeSize!= TRUE || m_bInputIsValidate == FALSE)
    {
        CWnd::OnLButtonUp(nFlags, point);
        return;
    }
    
    
    m_bEnterDragChangeSize = FALSE;  

    POINT pt = {point.x,point.y};
    ClientToScreen(&pt);

    if (m_bValidateInput == FALSE)
    {
        pt = m_OldSplitterPoint;
    }

    POINT ptTemp = {0,0};
    DrawSplitterLine(ptTemp,TRUE);

    BOOL bChangeWidth = ChangeClientWindowSize(pt);
    if (bChangeWidth == TRUE)
    { 
        //call client to Changed its size
        AfterSizeChanged();
    } 

    CWnd::OnLButtonUp(nFlags, point);
}
